package com.szxy.datetest;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @Author: zwz
 * @Date:2020/9/30 11:54
 * @Description:com.szxy.datetest
 * @Version:1.0
 * 新版日期 API 线程安全
 **/
public class NewDate {

    public static void main(String[] args) throws Exception {
        // 线程不安全
        //  SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");

        Callable<LocalDate> callable = new Callable<LocalDate>(){
            @Override
            public LocalDate call() throws Exception {
                return LocalDate.parse("20200909", dtf);
            }
        };
        List<Future<LocalDate>> results = new ArrayList<>();
        ExecutorService pool = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10; i++) {
            Future<LocalDate> future = pool.submit(callable);
            results.add(future);
        }

        for (Future<LocalDate> call : results) {
            try {
                System.out.println(call.get());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 关闭
        pool.shutdown();
    }
}
